<HTML>
<HEAD>
<TITLE>
RegistroDisco.cpp
</TITLE>
</HEAD>
<BODY>
<PRE>
<font color="blue">#include</font> <font color="maroon">"RegistroDisco.h"</font>

RegistroDisco<font color="black">:</font><font color="black">:</font>RegistroDisco<font color="black">(</font>TipoMemoria tipo, <font color="blue">int</font> tamanio, FILE<font color="black">*</font> fp<font color="black">)</font><font color="black">{</font>
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>tipoMemoria <font color="black">=</font> tipo;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>tamanio <font color="black">=</font> tamanio;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp <font color="black">=</font> fp;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>offset <font color="black">=</font> ftell<font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp<font color="black">)</font>;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>dato <font color="black">=</font> NULL;
<font color="black">}</font>

RegistroDisco<font color="black">:</font><font color="black">:</font>RegistroDisco<font color="black">(</font>TipoMemoria tipo, <font color="blue">int</font> tamanio, FILE<font color="black">*</font> fp, Dato <font color="black">*</font>dato<font color="black">)</font><font color="black">{</font>
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>tipoMemoria <font color="black">=</font> tipo;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>tamanio <font color="black">=</font> tamanio;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp <font color="black">=</font> fp;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>offset <font color="black">=</font> ftell<font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp<font color="black">)</font>;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>dato <font color="black">=</font> dato;
<font color="black">}</font>

<font color="blue">void</font> RegistroDisco<font color="black">:</font><font color="black">:</font>getAccesoDato<font color="black">(</font><font color="blue">void</font> <font color="black">*</font><font color="black">*</font>accesoDato<font color="black">)</font><font color="black">{</font>
    fseek<font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp,<font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>offset,SEEK_SET<font color="black">)</font>;
    <font color="black">*</font>accesoDato <font color="black">=</font> <font color="black">(</font><font color="blue">void</font><font color="black">*</font><font color="black">)</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp; 
<font color="black">}</font>

<font color="blue">void</font> RegistroDisco<font color="black">:</font><font color="black">:</font>leer<font color="black">(</font>Dato <font color="black">*</font>datoLeido, TipoSerializacion modo<font color="black">)</font><font color="black">{</font>
    <font color="blue">if</font><font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp<font color="black">=</font><font color="black">=</font>NULL<font color="black">)</font>
        <font color="blue">throw</font> <font color="blue">new</font> NullPointerException<font color="black">(</font><font color="black">)</font>;
    <font color="blue">unsigned</font> <font color="blue">int</font> bytesLeidos;
    <font color="blue">void</font> <font color="black">*</font>miDato <font color="black">=</font> NULL;
    <font color="blue">int</font> bytesDato <font color="black">=</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>tamanio;
    fseek<font color="black">(</font>fp,<font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>offset,SEEK_SET<font color="black">)</font>;
    <font color="blue">if</font><font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>tipoMemoria<font color="black">!</font><font color="black">=</font>FIJA<font color="black">)</font><font color="black">{</font>
        bytesLeidos <font color="black">=</font> fread<font color="black">(</font><font color="black">&</font>bytesDato, RegistroDisco<font color="black">:</font><font color="black">:</font>bytesControlTamanio, <font color="maroon">1</font>, fp<font color="black">)</font>;
        <font color="green">//if(bytesLeidos != RegistroDisco::bytesControlTamanio)</font>
        <font color="blue">if</font><font color="black">(</font><font color="black">!</font>bytesLeidos<font color="black">)</font>
            <font color="blue">throw</font> <font color="blue">new</font> DiscStorageException<font color="black">(</font>StorageException<font color="black">:</font><font color="black">:</font>lectura<font color="black">)</font>;
    <font color="black">}</font>   
    miDato <font color="black">=</font> malloc<font color="black">(</font>bytesDato<font color="black">)</font>;
    bytesLeidos <font color="black">=</font> fread<font color="black">(</font>miDato, bytesDato, <font color="maroon">1</font>, fp<font color="black">)</font>;  
    <font color="green">//if(bytesLeidos != this-&#62;tamanio)  </font>
    <font color="blue">if</font><font color="black">(</font><font color="black">!</font>bytesLeidos<font color="black">)</font>
        <font color="blue">throw</font> <font color="blue">new</font> DiscStorageException<font color="black">(</font>StorageException<font color="black">:</font><font color="black">:</font>lectura<font color="black">)</font>;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>tamanio <font color="black">=</font> bytesDato;
    <font color="blue">if</font><font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>tipoMemoria<font color="black">!</font><font color="black">=</font>FIJA<font color="black">)</font>
        <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>tamanio <font color="black">+</font><font color="black">=</font> Registro<font color="black">:</font><font color="black">:</font>bytesControlTamanio;
    datoLeido<font color="black">-</font><font color="black">&#62;</font>unserialize<font color="black">(</font>miDato, modo<font color="black">)</font>;
    free<font color="black">(</font>miDato<font color="black">)</font>;
<font color="black">}</font>

<font color="blue">void</font> RegistroDisco<font color="black">:</font><font color="black">:</font>escribir<font color="black">(</font><font color="blue">void</font> <font color="black">*</font>accesoDisco, TipoSerializacion modo<font color="black">)</font><font color="black">{</font>
    <font color="blue">if</font><font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp<font color="black">=</font><font color="black">=</font>NULL<font color="black">|</font><font color="black">|</font>accesoDisco<font color="black">=</font><font color="black">=</font>NULL<font color="black">)</font>
        <font color="blue">throw</font> <font color="blue">new</font> NullPointerException<font color="black">(</font><font color="black">)</font>;
    FILE <font color="black">*</font>aux <font color="black">=</font> <font color="black">(</font>FILE<font color="black">*</font><font color="black">)</font> accesoDisco;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>offset <font color="black">=</font> ftell<font color="black">(</font>aux<font color="black">)</font>;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>escribirEnDisco<font color="black">(</font>modo<font color="black">)</font>;
<font color="black">}</font>

<font color="blue">void</font> RegistroDisco<font color="black">:</font><font color="black">:</font>escribir<font color="black">(</font>TipoSerializacion modo<font color="black">)</font><font color="black">{</font>
    <font color="blue">if</font><font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp<font color="black">=</font><font color="black">=</font>NULL<font color="black">)</font>
        <font color="blue">throw</font> <font color="blue">new</font> NullPointerException<font color="black">(</font><font color="black">)</font>;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>escribirEnDisco<font color="black">(</font>modo<font color="black">)</font>;
<font color="black">}</font>

<font color="blue">void</font> RegistroDisco<font color="black">:</font><font color="black">:</font>escribir<font color="black">(</font><font color="blue">void</font> <font color="black">*</font>accesoDisco, Dato <font color="black">*</font>dato, TipoSerializacion modo<font color="black">)</font><font color="black">{</font>
    <font color="blue">if</font><font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp<font color="black">=</font><font color="black">=</font>NULL<font color="black">|</font><font color="black">|</font>accesoDisco<font color="black">=</font><font color="black">=</font>NULL<font color="black">)</font>
        <font color="blue">throw</font> <font color="blue">new</font> NullPointerException<font color="black">(</font><font color="black">)</font>;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>dato <font color="black">=</font> dato;
    FILE <font color="black">*</font>aux <font color="black">=</font> <font color="black">(</font>FILE<font color="black">*</font><font color="black">)</font> accesoDisco;
        <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>offset <font color="black">=</font> ftell<font color="black">(</font>aux<font color="black">)</font>;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>tamanio <font color="black">=</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>dato<font color="black">-</font><font color="black">&#62;</font>getTamanio<font color="black">(</font>modo<font color="black">)</font> <font color="black">+</font> Registro<font color="black">:</font><font color="black">:</font>bytesControlTamanio;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>escribirEnDisco<font color="black">(</font>modo<font color="black">)</font>;
<font color="black">}</font>

<font color="blue">void</font> RegistroDisco<font color="black">:</font><font color="black">:</font>borrar<font color="black">(</font><font color="black">)</font><font color="black">{</font>   
    <font color="blue">if</font><font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp<font color="black">=</font><font color="black">=</font>NULL<font color="black">)</font>
        <font color="blue">throw</font> <font color="blue">new</font> NullPointerException<font color="black">(</font><font color="black">)</font>;
    free<font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>dato<font color="black">)</font>;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>dato <font color="black">=</font> NULL;
<font color="black">}</font>

<font color="blue">void</font> RegistroDisco<font color="black">:</font><font color="black">:</font>escribirEnDisco<font color="black">(</font>TipoSerializacion modo<font color="black">)</font><font color="black">{</font>
    <font color="blue">unsigned</font> <font color="blue">int</font> bytesEscritos;
    fseek<font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>fp,<font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>offset,SEEK_SET<font color="black">)</font>;
    <font color="blue">if</font><font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>tipoMemoria<font color="black">!</font><font color="black">=</font>FIJA<font color="black">)</font><font color="black">{</font>
        <font color="blue">int</font> datosAEscribir <font color="black">=</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>dato<font color="black">-</font><font color="black">&#62;</font>getTamanio<font color="black">(</font>modo<font color="black">)</font>;
        bytesEscritos <font color="black">=</font> fwrite<font color="black">(</font><font color="black">&</font>datosAEscribir, Registro<font color="black">:</font><font color="black">:</font>bytesControlTamanio, <font color="maroon">1</font>, fp<font color="black">)</font>;
        <font color="green">//if(bytesEscritos != Registro::bytesControlTamanio)</font>
        <font color="blue">if</font><font color="black">(</font><font color="black">!</font>bytesEscritos<font color="black">)</font>
            <font color="blue">throw</font> <font color="blue">new</font> DiscStorageException<font color="black">(</font>StorageException<font color="black">:</font><font color="black">:</font>escritura<font color="black">)</font>;
    <font color="black">}</font>
    <font color="blue">void</font> <font color="black">*</font>datoAEscribir;
    datoAEscribir <font color="black">=</font> malloc<font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>dato<font color="black">-</font><font color="black">&#62;</font>getTamanio<font color="black">(</font>modo<font color="black">)</font><font color="black">)</font>;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>dato<font color="black">-</font><font color="black">&#62;</font>serialize<font color="black">(</font><font color="black">&</font>datoAEscribir, modo<font color="black">)</font>;
    bytesEscritos <font color="black">=</font> fwrite<font color="black">(</font>datoAEscribir, <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>dato<font color="black">-</font><font color="black">&#62;</font>getTamanio<font color="black">(</font>modo<font color="black">)</font>, <font color="maroon">1</font>, fp<font color="black">)</font>;
    <font color="green">//if(bytesEscritos != dato-&#62;getTamanio())</font>
    <font color="blue">if</font><font color="black">(</font><font color="black">!</font>bytesEscritos<font color="black">)</font>
            <font color="blue">throw</font> <font color="blue">new</font> DiscStorageException<font color="black">(</font>StorageException<font color="black">:</font><font color="black">:</font>escritura<font color="black">)</font>;
    free<font color="black">(</font>datoAEscribir<font color="black">)</font>;
<font color="black">}</font>

</PRE>
</BODY>
</HTML>
